/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.MapUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.util.Map;
/*     */ import java.util.concurrent.TimeUnit;
/*     */ import java.util.concurrent.locks.ReentrantLock;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImapCommandThrottle
/*     */ {
/*  29 */   private ImapCommand lastCommand = null;
/*  30 */   private int repeats = 0;
/*  31 */   private int repeatLimit = 0;
/*     */   private static final long REPEAT_TIME_THRESHOLD = 300000L;
/*  33 */   private static final Map<String, ReentrantLock> commandLock = MapUtil.newTimeoutMap(3600000L);
/*     */   private static final long LOCK_TIMEOUT = 600000L;
/*     */   
/*     */   public ImapCommandThrottle(int repeatLimit)
/*     */   {
/*  38 */     this.repeatLimit = repeatLimit;
/*     */   }
/*     */   
/*     */   public int getRepeatLimit() {
/*  42 */     return this.repeatLimit;
/*     */   }
/*     */   
/*     */   private boolean isCommandRepeated(ImapCommand command) {
/*  46 */     return (command != null) && (this.lastCommand != null) && (command.getCreateTime() - this.lastCommand.getCreateTime() < 300000L) && (command.isDuplicate(this.lastCommand));
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean isCommandThrottled(ImapCommand command)
/*     */   {
/*  52 */     if (this.repeatLimit <= 0)
/*  53 */       return false;
/*  54 */     if (command.throttle(this.lastCommand))
/*     */     {
/*  56 */       ZimbraLog.imap.debug("throttled by command");
/*  57 */       return true; }
/*  58 */     if (isCommandRepeated(command)) {
/*  59 */       this.repeats += 1;
/*  60 */       this.lastCommand = command;
/*  61 */       if (this.repeats > this.repeatLimit) {
/*  62 */         ZimbraLog.imap.debug("throttled by repeat");
/*  63 */         return true;
/*     */       }
/*  65 */       return false;
/*     */     }
/*     */     
/*  68 */     this.repeats = 1;
/*  69 */     this.lastCommand = command;
/*  70 */     return false;
/*     */   }
/*     */   
/*     */   public void reset()
/*     */   {
/*  75 */     this.repeats = 0;
/*  76 */     this.lastCommand = null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public ReentrantLock lock(String accountId)
/*     */     throws ImapThrottledException
/*     */   {
/*  87 */     ReentrantLock lock = null;
/*  88 */     synchronized (commandLock) {
/*  89 */       lock = (ReentrantLock)commandLock.get(accountId);
/*  90 */       if (lock == null) {
/*  91 */         lock = new ReentrantLock();
/*  92 */         commandLock.put(accountId, lock);
/*     */       }
/*     */     }
/*  95 */     boolean locked = false;
/*     */     try {
/*  97 */       locked = lock.tryLock(600000L, TimeUnit.MILLISECONDS);
/*     */     }
/*     */     catch (InterruptedException e) {}
/* 100 */     if (!locked) {
/* 101 */       throw new ImapThrottledException("Unable to obtain command lock " + lock.toString() + " aborting operation");
/*     */     }
/* 103 */     return lock;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/ImapCommandThrottle.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */